package com.shd.edanpei.presenter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;

import com.shd.edanpei.R;
import com.shd.edanpei.base.network.ApiConstant;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.QrcodeBean;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.ui.activity.CertificationActivity;
import com.shd.edanpei.ui.activity.CreateStepActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.PublishContract;
import com.shd.edanpei.ui.activity.MyTeamActivity;
import com.shd.edanpei.ui.activity.TeamActivity;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.util.contact.ContactBean;
import com.shd.edanpei.util.contact.ProductTypeListBean;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/17.
 * 选择申请区域
 */

public class PublishPresenter extends BasePresenterImpl<PublishContract.PublishIView>
        implements PublishContract.PublishIPresenter {

    private Bundle bundle = new Bundle();
    private CustomerBean customerBean = new CustomerBean();

    public PublishPresenter(PublishContract.PublishIView mView) {
        super(mView);
    }

    @Override
    public void onAddress() {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {
            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                customerBean.setArea(address);
                customerBean.setAreaCode(districtCode);
                customerBean.setGcityCode(cityCode);
                mBaseIView.setAddress(address);
            }
        });
    }

    @Override
    public void onCreate(int orderType) {
//        if (!DaoManager.getInstance().getUserBean().getIsReal()) {
//            //未实名认证
//            AlertDialog.Builder builder = new AlertDialog.Builder(mBaseIView.getContext());
//            builder.setTitle(mBaseIView.getContext().getString(R.string.unauthenticated))
//                    .setMessage("很抱歉，您还未实名认证，实名认证后方可发单。")
//                    .setNegativeButton(mBaseIView.getContext().getString(R.string.cancel), null)
//                    .setPositiveButton(mBaseIView.getContext().getString(R.string.to_authentication), new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            Bundle bundle = new Bundle();
//                            bundle.putInt(Constant.FROM, CertificationActivity.FORM_DATA);
//                            mBaseIView.startActivity(CertificationActivity.class, bundle);
//                        }
//                    }).show();
//            return;
//
//        }
        if (TextUtils.isEmpty(customerBean.getArea())) {
            customerBean.setArea("浙江省 杭州市 滨江区");
            customerBean.setAreaCode("330108");
            customerBean.setGcityCode("330100");
        }
        bundle.clear();
        bundle.putInt(Constant.ORDER_TYPE, orderType);
        bundle.putParcelable(Constant.DATA, customerBean);
        mBaseIView.startActivity(CreateStepActivity.class, bundle);
    }

    @Override
    public void getProductTypeList() {
        ApiCreator.getApi().getProductList()
                .compose(RxTransformer.<ProductTypeListBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ProductTypeListBean>() {
                    @Override
                    protected void onSuccess(ProductTypeListBean productTypeListBean) {
                        mBaseIView.getAdpster().setNewData(productTypeListBean.getData());
                       //ToastUtil.showToast(productTypeListBean.getData().get(0).getPic()+"");
                    }

                    @Override
                    public void onFinish() {
//                        mBaseIView.stopLoading();
                    }
                });

    }
}
